﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MicropleSoft.WmsApp.Library;
using MicropleSoft.WmsApp.Library.ServiceFacade;

namespace MicropleSoft.Controls
{
    public class DomainModelBinder
    {
        private static  void SetReadOnly(IDataBinder dataBinder,IBaseDomainObject baseDomainObject)
        {
            if (baseDomainObject.DomainObjectState == DomainEntityState.Added)
            {
                dataBinder.ReadOnly = false;
            }
            else
            {
                if (dataBinder.IsPrimaryKey)
                {
                    dataBinder.ReadOnly = true;
                }
                else
                {
                    dataBinder.ReadOnly = false;
                }
            }
        }

        public static void BindingDomainObjects(Control control,IBaseDomainObject baseDomainObject)
        {
            foreach (Control ctl in control.Controls)
            {
                if(ctl is IDataBinder)
                {
                    var dataBinder = (IDataBinder)ctl;
                    if(String.IsNullOrEmpty( dataBinder.PropertyName))
                    {
                        continue;
                    }
                    dataBinder.ExcuteDataBinding(baseDomainObject);
                    SetReadOnly(dataBinder, baseDomainObject);
                }
                else
                {
                    if(ctl.Controls.Count>0)
                    {
                        BindingDomainObjects(ctl, baseDomainObject);
                    }
                }
            }
        }
    }
}
